using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component.Operation
{
	public class Game7Component : OperationComponent
	{
		private Transform btnBack;

		private Transform needleDemo;

		private List<Transform> efcs = new List<Transform>();

		private NeedleObject touchNeedle;

		private Vector2 lastTouchPoint;

		private float difX;

		private float difY;

		private void Awake()
		{
			btnBack = base.transform.Find("BtnBack");
			needleDemo = base.transform.Find("Needle");
			int num = base.transform.childCount - 3;
			List<int> list = new List<int>();
			for (int i = 0; i < num; i++)
			{
				list.Add(i + 1);
			}
			int num2 = Random.Range(5, 8);
			for (int j = 0; j < num2; j++)
			{
				int index = Random.Range(0, list.Count);
				Transform transform = base.transform.Find("Wound" + list[index]);
				list.RemoveAt(index);
				transform.gameObject.SetActive(true);
				Transform transform2 = Object.Instantiate(needleDemo.gameObject).transform;
				transform2.name = "Needle";
				transform2.SetParent(transform);
				transform2.SetLocalRotationZ(0f);
				transform2.SetLocalXYZ(0f, 0.437f, -1E-05f);
				transform2.gameObject.SetActive(true);
				NeedleObject needleObject = transform2.gameObject.AddComponent<NeedleObject>();
				needleObject.component = this;
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (displayer == null)
			{
				return;
			}
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 1);
			ResetTouchNeedle();
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btnBack)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					displayer.ChangeType(OperationDisplayerItem.Type.Main);
				}
				else if (raycastHit2D.collider.transform.name.Equals("Needle"))
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					touchNeedle = raycastHit2D.collider.transform.GetComponent<NeedleObject>();
					if (touchNeedle != null)
					{
						touchNeedle.IsTouch(true);
					}
				}
			}
			lastTouchPoint = touchEventData.curPos;
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			if (touchNeedle != null)
			{
				difX = touchEventData.curPos.x - lastTouchPoint.x;
				difY = touchEventData.curPos.y - lastTouchPoint.y;
				touchNeedle.AddProgress(difX, difY);
			}
			lastTouchPoint = touchEventData.curPos;
		}

		public void Complete(Vector3 pos)
		{
			Singleton<AudioManager>.Instance.PlaySound("sound_blink");
			Transform transform = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", pos + new Vector3(0f, 0f, -1E-05f), 3f);
			transform.SetParent(base.transform);
			efcs.Add(transform);
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			ResetTouchNeedle();
		}

		private void ResetTouchNeedle()
		{
			if (touchNeedle != null)
			{
				touchNeedle.IsTouch(false);
			}
			touchNeedle = null;
		}

		private void OnDestroy()
		{
			for (int i = 0; i < efcs.Count; i++)
			{
				if (efcs[i] != null)
				{
					Singleton<GamePoolManager>.Instance.Despawn(efcs[i]);
				}
			}
			efcs.Clear();
		}
	}
}
